package com.bawei.yuekao.fragment;


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bawei.yuekao.R;
import com.bawei.yuekao.adapter.SongAdapter;
import com.bawei.yuekao.entity.Song;
import com.bawei.yuekao.sql.MusicHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class LoveMusicFragment extends Fragment {
    private ListView listView;
    private List<Song> list = new ArrayList<>();
    private SongAdapter songAdapter;
    private MusicHelper musicHelper ;//!!!!!千万不要着么写 空指针
    private SQLiteDatabase sqLiteDatabase;


    public LoveMusicFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_love_music, container, false);
        listView = view.findViewById(R.id.lv);
        musicHelper = new MusicHelper(getContext());
        sqLiteDatabase = musicHelper.getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("music", null, null, null, null, null, null);
        if(cursor != null){
            while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String singer = cursor.getString(cursor.getColumnIndex("singer"));
                String data = cursor.getString(cursor.getColumnIndex("data"));
                String album = cursor.getString(cursor.getColumnIndex("album"));
                list.add(new Song(name,singer,data,album));
            }
        }
        songAdapter = new SongAdapter(list,getContext());
        listView.setAdapter(songAdapter);
        return view;
    }

}
